package io.renren.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.shop.entity.GoodsMsg;
import io.renren.modules.shop.entity.GoodsOrder;
import io.renren.modules.shop.entity.WxUser;
import io.renren.modules.shop.service.GoodsOrderService;
import io.renren.modules.shop.service.WxUserService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("goods_order")
@Api(value = "商品订单总表Controller",tags = {("商品订单总表接口")})
public class GoodsOrderController extends AbstractController {
    @Autowired
    private GoodsOrderService goodsOrderService;
    @Autowired
    private WxUserService wxUserService;
    @ApiOperation(value = "查询分页")
    @PostMapping("page")
    public R getPage(@RequestBody GoodsOrder goodsOrder, @RequestParam Integer size, @RequestParam Integer page) {
        IPage<GoodsOrder> iPage = new Page<>(page,size);
        IPage<GoodsOrder> goodsCouponIPage = goodsOrderService.page(iPage, new QueryWrapper<>(goodsOrder));
        PageUtils pageResult = new PageUtils(goodsCouponIPage);
        List<GoodsOrder> list = (List<GoodsOrder>) pageResult.getList();
        for (GoodsOrder order : list) {
            WxUser wxUser = wxUserService.getOne(new QueryWrapper<WxUser>().lambda().eq(WxUser::getOpenid, order.getOpenid()));
            if (wxUser != null) {
                order.setOpenid(wxUser.getNickName());
            }
        }
        pageResult.setList(list);
        return R.ok().put("page",pageResult);
    }


    @ApiOperation(value = "新增详情订单")
    @PostMapping("add")
    public R addNew(@RequestBody GoodsOrder goodsOrder) {
        boolean flag = goodsOrderService.save(goodsOrder);
        return R.ok().put("flag",flag);
    }

    @ApiOperation(value = "查询所有数据")
    @PostMapping("list")
    public R getList(@RequestBody GoodsOrder goodsOrder) {
        List<GoodsOrder> list = goodsOrderService.list(new QueryWrapper<>(goodsOrder));
        return R.ok().put("list",list);
    }

    @ApiOperation(value = "根据ids删除记录")
    @PostMapping("delete")
    public R del(@RequestParam Integer[] ids) {
        boolean flag = goodsOrderService.removeByIds(Arrays.asList(ids));
        return R.ok().put("flag",flag);
    }

    @ApiOperation(value = "购物车选择商品购买")
    @PostMapping("buyCar")
    public R buyCar(@RequestBody List<GoodsMsg> goodsMsgs, @RequestParam Integer couponid) {
        WxUser wxUser = ShiroUtils.getWxUser();
        goodsOrderService.buyCar(goodsMsgs,couponid,wxUser.getOpenid());
    return R.ok();
    }

    @ApiOperation(value = "查询用户自身订单")
    @GetMapping("getOrder")
    public R getOrder() {
        List<GoodsOrder> list = goodsOrderService.getList(getOpenId());
        return R.ok().put("list", list);
    }


}
